<template>
  <div class="app-container">
    <!-- 添加或修改资产记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" v-if="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="60">
          <el-col :span="12">
            <el-form-item label="授课老师" prop="teacherId">
              <group-select class="form-item-input-style" v-model="form.teacherId" :group_list="['user']"
                :group_query="teacherGroupQuery" placeholder="请选择授课老师" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="学员" prop="studentId">
              <group-select class="form-item-input-style" v-model="form.studentId" :group_list="['student']"
                placeholder="请选择学员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程" prop="curriculumId">
              <group-select class="form-item-input-style" v-model="form.curriculumId" :group_list="['curriculum']"
                placeholder="请选择课程" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课时" prop="lessonHours">
              <el-input-number :disabled="form.id" v-model="form.lessonHours" :min="1" :max="99999"
                placeholder="请输入课时"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签到状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择签到状态">
                <el-option v-for="dict in dict.type.kj_student_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布状态" prop="release">
              <el-select v-model="form.release" placeholder="请选择发布状态">
                <el-option label="未发布" value="0"></el-option>
                <el-option label="已发布" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="签到照片" prop="photo">
              <ImageUpload2 :limit="4" v-model="form.photo" />
            </el-form-item>
          </el-col>
          

          <el-col :span="24">
            <el-form-item label="签到视频" prop="video">
              <fileUpload :file-size="512" :is-url="true" v-model="form.video" :fileType="fileType"  :limit="1"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签到时间" prop="signDate">
              <el-date-picker clearable v-model="form.signDate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择签到时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="课程时间" prop="courseDuration">
              <el-date-picker clearable v-model="form.courseDuration" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择课程时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <span style="display: inline-block;">
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startHour">
                <el-time-select size="small" placeholder="起始时间" v-model="form.startHour"
                  :picker-options="{ start: '06:00', step: '00:10', end: '23:00' }">
                </el-time-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束时间" prop="endHour">
                <el-time-select size="small" placeholder="结束时间" v-model="form.endHour"
                  :picker-options="{ start: '06:30', step: '00:10', end: '23:00', minTime: form.startHour }">
                </el-time-select>
              </el-form-item>
            </el-col>
          </span>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="评价" prop="evaluate">
              <el-input v-model="form.evaluate" type="textarea" :rows="5" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="评价" prop="evaluate">
              <editor v-model="form.evaluate" :min-height="192" :readOnly="disabled" />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" v-if="!disabled">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/jiaopei/record";
import groupSelect from "@/views/components/groupSelect";
import store from '@/store'
export default {
  name: "Dialog",
  dicts: ['kj_student_status'],
  components: {
    groupSelect,
  },
  computed: {
    teacherGroupQuery() {
      var value = {
        'user': {
          teacher: '0,1'
        }
      }
      return value
    }
  },
  data() {
    return {
      fileType: ['mp4', 'flv'],
      // 是否禁用表单
      disabled: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        teacherId: [
          { required: true, message: "老师不能为空", trigger: "blur" }
        ],
        lessonHours: [
          { required: true, message: "课时不能为空", trigger: "blur" }
        ],
        studentId: [
          { required: true, message: "学员不能为空", trigger: "blur" }
        ],
        curriculumId: [
          { required: true, message: "课程不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "签到状态", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    /** 打开资产记录弹框 */
    async show(id, disabled, form) {
      this.reset();
      this.disabled = disabled
      this.form = this.$common.deepMergeExistingProperties(this.form, form);

      if (this.disabled) {
        this.title = "详情"
      } else if (id) {
        await getRecord(id).then(response => {
          this.form = response.data;
          this.form.studentId = `${this.form.studentId}`
          this.form.teacherId = `${this.form.teacherId}`
          this.form.curriculumId = `${this.form.curriculumId}`
        });
        this.title = "修改"
      } else {
        this.title = "新增"
      }
      this.open = true
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      const date = new Date();
      this.form = {
        id: undefined,
        teacherName: undefined,
        teacherId: undefined,
        studentId: undefined,
        studentName: undefined,
        curriculumId: undefined,
        curriculumName: undefined,
        courseDuration: undefined,
        lessonHours: 1,
        startHour: undefined,
        endHour: undefined,
        status: "2",
        release: "0",
        photo: undefined,
        signDate: this.formatDateTime(date),
        evaluate: undefined,
        remarks: undefined,
        createdDate: undefined
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('refresh');
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('refresh');
            });
          }
        }
      });
    },
    formatDateTime(date) {
      // 格式化日期时间为字符串，例如：'2023-04-01 12:00:00'
      // 根据需要自定义格式
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
    }
  }
};
</script>
